﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Legend.Component.Interface;
using Microsoft.Xna.Framework;
using Legend.Object;
using Legend.Component.Common;
using Legend.Component.Bullet;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Legend.Component.Hero
{
    public class FirstWeapon : IWeapon
    {
        private float fireSpeed;
        private bool fired;
        private int elapsedNonFireTime;
        private bool enabled = true;

        public float FireSpeed
        {
            get { return fireSpeed; }
            set { fireSpeed = value; }
        }

        public override Type UniqueType
        {
            get { return typeof(FirstWeapon); }
        }

        public bool Enable
        {
            get { return enabled; }
            set { enabled = value; }
        }

        public FirstWeapon()
        {
            elapsedNonFireTime = 999999;
        }

        public override void Fire()
        {
            fired = true;
        }

        public override void Update(GameTime gameTime, ContentManager contentManager)
        {
            elapsedNonFireTime += gameTime.ElapsedGameTime.Milliseconds;

            if (!enabled)
            {
                return;
            }

            if (fired)
            {
                if (elapsedNonFireTime < fireSpeed * 1000)
                {
                    // 아직 발사할 수 없다.
                }
                else
                {
                    // 이제 발사 가능
                    IMovement movement = Owner.FindComponent<IMovement>();

                    GameObject bullet = new GameObject();

                    Vector3 position = movement.Position;
                    position.Z = 10;
                    position.Y += 30;

                    Bullet.Movement bulletMovement = bullet.CreateComponent<Bullet.Movement>();
                    bulletMovement.Velocity = new Vector3(0, 600, 0);
                    bulletMovement.Position = position;
                    bulletMovement.Timeout = 2000;

                    BulletRenderable renderable = bullet.CreateComponent<BulletRenderable>();
                    BoundingBox boundingBox = renderable.LoadResource(contentManager);
                    renderable.SetTextureUV(new Vector2(0, 0), new Vector2(1f / 8f, 57f / 128f));

                    Collision collision = bullet.CreateComponent<Collision>();
                    collision.LocalBoundingBox = boundingBox;

                    WeaponDamaging damaging = bullet.CreateComponent<WeaponDamaging>();
                    damaging.Damage = 100;

                    GameObjectManager.RegistGameObject(bullet);
                    elapsedNonFireTime = 0;
                }
                fired = false;
            }
        }
    }
}
